//
//  RTJMyclientDetailsController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2019/1/2.
//  Copyright © 2019年 leewei. All rights reserved.
//

#import "RTJMyclientDetailsController.h"
#import "RTJBidRecordsCell.h"
@interface RTJMyclientDetailsController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;
@property(nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSDictionary *dic;

@end

@implementation RTJMyclientDetailsController

-(NSMutableArray *)data {
    
    if (!_data) {
        _data = [NSMutableArray array];
    }
    
    return _data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight=300;
    
    
        self.currentPage = 1;
        self.tableView.mj_header = [CustomRefreshGifHeader headerWithRefreshingBlock:^{
    
            self.currentPage = 1;
            [self loadData];
    
        }];
    
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
    
            self.currentPage ++;
            [self loadData];
    
        }];
    
        [self.tableView.mj_header beginRefreshing];
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    
    if (self.data.count == 0) {
        
        [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
        
    }
    
    [RTJNetWork asyncRequestWithURL:@"user/my_customer_info" params:@{@"page":@(_currentPage),@"id":_idStr} method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_dianhua"] style:UIBarButtonItemStylePlain target:self action:@selector(dianhua)];

                self.dic = data[@"data"];
                NSArray *arr = [NSArray array];
                arr = data[@"data"][@"list"];
                
                if (arr.count > 0) {
                    
                    if (self.currentPage == 1) {
                        
                        [self.data removeAllObjects];
                    }
                    
                    [self.data addObjectsFromArray:arr];
                    
                    if (arr.count < ZXPAGE_SIZE) {
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                    
                }else{
                    
                    if (self.currentPage == 1) {
                        
                        
                        [self.data removeAllObjects];
                        
                        [self.view showEmptyViewWithStr:@"暂无相关数据" image:[UIImage imageNamed:@"kbylogo"] callback:^{
                            [self loadData];
                        }];
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        
                    }else{
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        
                    }
                }
                
                [self.tableView reloadData];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
                [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];
            
        }
        
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.data.count!=0 ? self.data.count+1 : 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJBidRecordsCell";
    
    RTJBidRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        if (indexPath.row ==0 ) {
            
             if (!cell) {
                 
                 cell = [[NSBundle mainBundle] loadNibNamed:@"RTJBidRecordsCell" owner:self options:nil][6];
             }
            
            cell.chongzhi.text = [NSString stringWithFormat:@"充值金额：%@元",_dic[@"carry_money"]];
            cell.touzi.text = [NSString stringWithFormat:@"出借金额：%@元",_dic[@"load_money"]];
            cell.tixian.text = [NSString stringWithFormat:@"提现金额：%@元",_dic[@"recharge"]];

        }else{
            
            if (!cell) {
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJBidRecordsCell" owner:self options:nil][4];
            }
            
            cell.touziPeople.text = [NSString stringWithFormat:@"%@",_data[indexPath.row-1][@"name"]];
            cell.touziTotal.text = [NSString stringWithFormat:@"%@元",_data[indexPath.row-1][@"money"]];
            cell.touziZhuce.text = [NSString stringWithFormat:@"出借时间：%@",_data[indexPath.row-1][@"create_time_format"]];
            
        }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}



-(void)dianhua{
    
    NSString *phone = [NSString stringWithFormat:@"%@",_dic[@"mobile"]];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确认拨打电话" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phone]];
        [[UIApplication sharedApplication] openURL:url];
        
        
    }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

@end
